<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/4/12
 *  Q Q: 505413
 * Time: 13:20
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\ConfigModel;
use think\Db;
class Config extends Base
{
    //获取配置参数
    public function index()
    {
        $configModel = new ConfigModel();
        $list = $configModel->getAllConfig();
        $config = [];
        foreach ($list as $k => $v) {
            $config[trim($v['name'])] = $v['value'];
        }
        $this->assign('config',$config);
        return $this->fetch();
    }
    //批量保存配置
    public function configEdit()
    {
        $config = request()->param();
        $config = $config['config'];
        if(request()->isAjax()){
            if(!empty($config['web_site_close']) && $config['web_site_close'] == 'on'){
                $config['web_site_close'] = '1';
            }else {
                $config['web_site_close'] = '0';
            }
            $configModel = new ConfigModel();
            if($config && is_array($config)){
                foreach ($config as $name => $value) {
                    $map = array('name' => $name);
                    $data  = $configModel->SaveConfig($map,$value);
                }
            }
            if(false === $data){
                return json(['code' => -1, 'url' => 'config/index', 'msg' => '修改配置失败']);
            }else{
                return json(['code' => 1, 'url' => 'config/index', 'msg' => '保存成功']);
            }
        }
    }
    //上传logo
    public function uploadLogo()
    {
        $file = request()->file('file');

        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/logo');//指定上传路径
            $name = $info->getSaveName(); // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $imgSize = $info->getSize();
            $format = array('jpg','png','gif');//定义上传文件类型
            if($imgSize >= '102400'){
                return json(['code' => -2, 'url' => 'config/index', 'msg' => '图片大小不能超过10M']);
            }
            if(false === $info){
                return json(['code' => -1, 'url' => 'config/index', 'msg' => '上传失败，请重试']);
            }else{
                return json(['code' => 1, 'url' => 'config/index', 'msg' => 'Logo上传成功','name' => $name]);
            }
        }
    }
}